package com.supcalls;

import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.function.Supplier;

public class MethodReferenceExample {
    public static void main(String[] args) {
        // 1. 静态方法引用：ClassName::staticMethod
        List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
        names.forEach(MethodReferenceExample::printUpperCase);

        // 2. 实例方法引用：instance::instanceMethod
        MethodReferenceExample example = new MethodReferenceExample();
        names.forEach(example::printLowerCase);

        // 3. 特定类型的实例方法引用：Class::instanceMethod
        names.forEach(String::toUpperCase); // 等价于 name -> name.toUpperCase()

        // 4. 构造方法引用：Class::new
        Supplier<MethodReferenceExample> constructorRef = MethodReferenceExample::new;
        MethodReferenceExample instance = constructorRef.get();

        // 5. 引用构造方法创建对象
        Function<String, Person> personConstructor = Person::new;
        Person person = personConstructor.apply("John");
        System.out.println("Created Person: " + person.getName());
    }

    // 静态方法
    public static void printUpperCase(String str) {
        System.out.println(str.toUpperCase());
    }

    // 实例方法
    public void printLowerCase(String str) {
        System.out.println(str.toLowerCase());
    }

    // Person 类
    static class Person {
        private String name;

        // 构造方法
        public Person(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }
    }
}
